#include "TCPClient.h"
#include "util.h"
#include <cstdlib>
#include <unistd.h>
#include <iostream>

using namespace qy;

int main(int argc, char *argv[])
{
	if(argc < 3)
	{
		std::cout << "Usage: " << argv[0] << " server port\n";
		return -1;
	}
	TCPClient client(argv[1], atoi(argv[2]));
	if(client.fail())
	{
		std::cout << "Connect failed\n";
		return -1;
	}
	std::cout << "Connected\n";
	std::string message;
	while(std::cin >> message)
	{
		if(message == "88")
		{
			client.close();
			break;
		}
		if(client.canSend())
		{
			client.send(message.c_str(), message.length());
		}
		else
		{
			break;
		}
		while(client.canReceive())
		{
			char buf[1025];
			int size = client.receive(buf, 1024);
			buf[size] = '\0';
			log_sys("Receive: %s", buf);
			sleep(1);
		}
	}
	log_sys("Closed");
	return 0;
}
